<template>
  <div class="channel-edit">
    <div class="my-channel channel">
      <!-- 编辑/标题 -->
      <van-cell
       :border="false"
       title="我的频道" >
        <template>
          <van-button @click="isEdit = !isEdit" color="red"
          plain size="mini" round>
          {{isEdit? '完成':'编辑'}}
          </van-button>
        </template>
      </van-cell>
      <!-- 我的频道列表 -->
      <van-grid :gutter="10">
        <van-grid-item
        @click="onClickMyItem(index,item.id)"
         :icon="(isEdit && index !=0)?'close':''"
         v-for="(item,index) in myChannels"
         :class="{ active: index===activeIndex}"
         :key="item.id"
         :text="item.name" />
      </van-grid>
    </div>
    <div class="channel recommend-channel">
      <van-cell :border="false" title="推荐频道" />
       <!-- 推荐频道 -->
      <van-grid :gutter="10" direction="horizontal" icon-size="0.32rem">
        <van-grid-item icon="plus" @click="addToMyChannels(item)" v-for="item in recommendChannels" :key="item.id"
         :text="item.name" />
      </van-grid>
    </div>
  </div>
</template>

<script>
import { getAllChannels, addUserChannel, deleteUserChannel } from '@/api/channel.js'
import { Toast } from 'vant'
import { mapState } from 'vuex'
import { setItem } from '@/utils/storage.js'
export default {
  props: {
    myChannels: {
      type: Array,
      required: true
    },
    activeIndex: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      allChannels: [],
      isEdit: false
    }
  },

  created () {
    this.loadAllChannels()
  },
  computed: {
    // 推荐频道数据
    // recommendChannels () {
    //   // 对 allChonnels进行筛选
    //   const recommendArr = this.allChannels.filter(channelItem => {
    //     // myChannels 数组 是否包含 channelItem
    //     const flag = this.myChannels.some(myItem => {
    //       return myItem.id === channelItem.id
    //     })
    //     return !flag
    //   })
    //   // 筛选 什么样的数据
    //   // allChonnels 每一项的数据 判断 是否在 myChannels里
    //   // 如果在myChannels里 属于 我的频道 不属于推荐 不要的
    //   // 如果不在myChannels里 不属于 我的频道 属于推荐 要的
    //   return recommendArr
    // }

    recommendChannels () {
      return this.allChannels.filter(channelItem => {
        return !this.myChannels.some(myItem => myItem.id === channelItem.id)
      })
    },
    ...mapState(['user'])
  },
  methods: {
    async loadAllChannels () {
      try {
        const { channels } = await getAllChannels()
        this.allChannels = channels
      } catch (err) {
        Toast('异常')
      }
    },
    // 点击推荐频道
    async addToMyChannels (item) {
      // this.myChannels = [] X
      this.myChannels.push(item)
      // 数据持久化
      // 判断用户是否登录
      if (this.user) {
        try {
          await addUserChannel({
            id: item.id,
            seq: this.myChannels.length
          })
          Toast('添加成功')
        } catch (err) {
          Toast('添加失败')
        }
      } else {
        setItem('HMTT-CHANNELS', this.myChannels)
      }
    },
    // 点击我的频道
    onClickMyItem (index, id) {
      if (this.isEdit) {
        if (index === 0) return
        // 当前处于编辑 做的事情是 删除
        if (index <= this.activeIndex) {
          this.$emit('update-active', this.activeIndex - 1, true)
        }
        this.myChannels.splice(index, 1)
        this.deleteStorage(id)
      } else {
        // 当前处于编辑 做的事情是 跳转
        //  能够触发当前组件标签上的一个自定义事件
        this.$emit('update-active', index, false)
      }
    },
    // 删除持久化
    async deleteStorage (id) {
      // console.log(id)
      if (this.user) {
        try {
          await deleteUserChannel(id)
          console.log('发请求')
          Toast.success('删除成功')
        } catch (err) {
          Toast.fail('删除失败')
        }
      } else {
        setItem('HMTT-CHANNELS', this.myChannels)
      }
    }
  }
}
</script>

<style scoped lang='less'>
.channel-edit{
  padding-top: 90px;
}
.channel{
  /deep/.van-grid{
    .van-grid-item__content{
      width: 160px;
      height: 86px;
      background-color: #F4F5F6;
      border-radius: 6px;
      .van-grid-item__text{
        font-size: 24px;
        color: #222;
      }
    }
    .van-grid-item__content::after{
      border: none;
    }
  }
}
// 我的频道
.my-channel{
  /deep/.van-grid-item__icon+.van-grid-item__text{
    margin-top: 0px;
  }
  /deep/.active{
    .van-grid-item__text{
      color: red !important;
    }
  }
  .van-cell{
    display: flex;
    align-items: center;
  }
  .van-button{
    width: 103px;
    height: 50px;
  }
  /deep/.van-icon-close{
    position: absolute;
    font-size: 32px;
    right: -10px;
    top: -10px;
  }
}
</style>
